package net.gxnun.xiaoyuan.core.share;

import java.util.HashMap;
import java.util.Map;

import net.gxnun.xiaoyuan.core.message.request.EventMsg;
import net.gxnun.xiaoyuan.core.message.request.ImageMsg;
import net.gxnun.xiaoyuan.core.message.request.LinkMsg;
import net.gxnun.xiaoyuan.core.message.request.LocationMsg;
import net.gxnun.xiaoyuan.core.message.request.SoundMsg;
import net.gxnun.xiaoyuan.core.message.request.TextMsg;
import net.gxnun.xiaoyuan.core.message.response.ResponseMusicMsg;
import net.gxnun.xiaoyuan.core.message.response.ResponsePictureMsg;
import net.gxnun.xiaoyuan.core.message.response.ResponseTextMsg;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * 获得各个XSteam实例
 * 这是一个单例，通过getInstance()获得实例。
 * @author yesheng
 *
 */
public class MessageToXStreamUtils {
    
    private XStream responseTextStream = null;
    private XStream responseMusicStream = null;
    private XStream responsePictureStream = null;
    private XStream textStream = null;
    private XStream eventStream = null;
    private XStream imageStream = null;
    private XStream locationStream = null;
    private XStream linkStream = null;
    private XStream voiceStream = null;
    
    private Map<String,XStream> xsMap = new HashMap<String,XStream>();
    
    private static MessageToXStreamUtils instance = null;
    
    /**
     * 获取实例
     * @return
     */
    public static MessageToXStreamUtils getInstance(){
        if(instance == null){
            instance = new MessageToXStreamUtils();
        }
        return instance;
    }
    
    private MessageToXStreamUtils(){
        initStream();
        initMap();
    }
    
    /**
     * 根据类型返回相应的Stream
     * @param msgType
     * @return
     */
    public XStream getXStream(String msgType){
        return xsMap.get(msgType);
    }
    
    
    /**
     * 初始化Map
     */
    private void initMap(){
        xsMap.put(MsgType.Event, eventStream);
        xsMap.put(MsgType.Image, imageStream);
        xsMap.put(MsgType.Link, linkStream);
        xsMap.put(MsgType.Location, locationStream);
        xsMap.put(MsgType.Text, textStream);
        xsMap.put(MsgType.Voice, voiceStream);
        xsMap.put(MsgType.VoiceResponse, responseMusicStream);
        xsMap.put(MsgType.ImageResponse, responsePictureStream);
        xsMap.put(MsgType.TextResponse, responseTextStream);
    }
    
    /**
     * 初始化各种Stream，方便转换
     */
    private void initStream(){
        initResponseMusicStream();
        initResponseTextStream();
        initResponsePictureStream();
        initTextStream();
        initEventStream();
        initImageStream();
        initLocationStream();
        initLinkStream();
        initLinkStream();
        initVoiceStream();
    }
    
    private XStream initResponseMusicStream(){
        if(responseMusicStream == null){
            responseMusicStream = new XStream(new DomDriver());
            responseMusicStream.alias("xml", ResponseMusicMsg.class);
            responseMusicStream.aliasField("ToUserName", ResponseMusicMsg.class, "toUserName");
            responseMusicStream.aliasField("FromUserName", ResponseMusicMsg.class, "fromUserName");
            responseMusicStream.aliasField("CreateTime", ResponseMusicMsg.class, "createTime");
            responseMusicStream.aliasField("MsgType", ResponseMusicMsg.class, "msgType");
            responseMusicStream.aliasField("MusicUrl", ResponseMusicMsg.class, "musicUrl");
            responseMusicStream.aliasField("HQMusicUrl", ResponseMusicMsg.class, "hQMusicUrl");
            responseMusicStream.aliasField("FuncFlag", ResponseMusicMsg.class, "funcFlag");
        }
        
        return responseMusicStream;
        
    }
    
    private XStream initResponseTextStream(){
        
        if(responseTextStream == null){
            responseTextStream = new XStream(new DomDriver());
            responseTextStream.alias("xml", ResponseTextMsg.class);
            responseTextStream.aliasField("ToUserName", ResponseTextMsg.class, "toUserName");
            responseTextStream.aliasField("FromUserName", ResponseTextMsg.class, "fromUserName");
            responseTextStream.aliasField("CreateTime", ResponseTextMsg.class, "createTime");
            responseTextStream.aliasField("MsgType", ResponseTextMsg.class, "msgType");
            responseTextStream.aliasField("Content", ResponseTextMsg.class, "content");
            responseTextStream.aliasField("FuncFlag", ResponseTextMsg.class, "funcFlag");
        }
        
        return responseTextStream;
        
    }
    
    
    
    private XStream initResponsePictureStream(){
        
        if(responsePictureStream == null){
            responsePictureStream = new XStream(new DomDriver());
            responsePictureStream.alias("xml", ResponsePictureMsg.class);
            responsePictureStream.aliasField("ToUserName", ResponsePictureMsg.class, "toUserName");
            responsePictureStream.aliasField("FromUserName", ResponsePictureMsg.class, "fromUserName");
            responsePictureStream.aliasField("CreateTime", ResponsePictureMsg.class, "createTime");
            responsePictureStream.aliasField("MsgType", ResponsePictureMsg.class, "msgType");
            responsePictureStream.aliasField("ArticleCount", ResponsePictureMsg.class, "articleCount");
            responsePictureStream.aliasField("Articles", ResponsePictureMsg.class, "articles");
            responsePictureStream.aliasField("Title", ResponsePictureMsg.class, "title");
            responsePictureStream.aliasField("Description", ResponsePictureMsg.class, "description");
            responsePictureStream.aliasField("PicUrl", ResponsePictureMsg.class, "picUrl");
            responsePictureStream.aliasField("Url", ResponsePictureMsg.class, "url");
        }
        
        return responsePictureStream;
        
    }
    
    
    private XStream initTextStream(){
        
        if(textStream == null){
            textStream = new XStream(new DomDriver());
            textStream.alias("xml", TextMsg.class);
            textStream.aliasField("ToUserName", TextMsg.class, "toUserName");
            textStream.aliasField("FromUserName", TextMsg.class, "fromUserName");
            textStream.aliasField("CreateTime", TextMsg.class, "createTime");
            textStream.aliasField("MsgType", TextMsg.class, "msgType");
            textStream.aliasField("Content", TextMsg.class, "content");
            textStream.aliasField("MsgId", TextMsg.class, "msgId");
        }
        
        return textStream;
        
    }
    
    private XStream initEventStream(){
        if(eventStream == null){
            eventStream = new XStream(new DomDriver());
            eventStream.alias("xml", EventMsg.class);
            eventStream.aliasField("ToUserName", EventMsg.class, "toUserName");
            eventStream.aliasField("FromUserName", EventMsg.class, "fromUserName");
            eventStream.aliasField("CreateTime", EventMsg.class, "createTime");
            eventStream.aliasField("MsgType", EventMsg.class, "msgType");
            eventStream.aliasField("Event", EventMsg.class, "event");
            eventStream.aliasField("EventKey", EventMsg.class, "eventKey");
        }
        return eventStream;
    }
    
    private XStream initImageStream(){
        if(imageStream == null){
            imageStream = new XStream(new DomDriver());
            imageStream.alias("xml", ImageMsg.class);
            imageStream.aliasField("ToUserName", ImageMsg.class, "toUserName");
            imageStream.aliasField("FromUserName", ImageMsg.class, "fromUserName");
            imageStream.aliasField("CreateTime", ImageMsg.class, "createTime");
            imageStream.aliasField("MsgType", ImageMsg.class, "msgType");
            imageStream.aliasField("PicUrl", ImageMsg.class, "picUrl");
            imageStream.aliasField("MsgId", ImageMsg.class, "msgId");
            imageStream.aliasField("MediaId", ImageMsg.class, "mediaId");
        }
        
        return imageStream;
    }
    
    private XStream initLocationStream(){
        if(locationStream == null){
            locationStream = new XStream(new DomDriver());
            locationStream.alias("xml", LocationMsg.class);
            locationStream.aliasField("ToUserName", LocationMsg.class, "toUserName");
            locationStream.aliasField("FromUserName", LocationMsg.class, "fromUserName");
            locationStream.aliasField("CreateTime", LocationMsg.class, "createTime");
            locationStream.aliasField("MsgType", LocationMsg.class, "msgType");
            locationStream.aliasField("MsgId", LocationMsg.class, "msgId");
            locationStream.aliasField("Location_X", LocationMsg.class, "location_X");
            locationStream.aliasField("Location_Y", LocationMsg.class, "location_Y");
            locationStream.aliasField("Scale", LocationMsg.class, "scale");
            locationStream.aliasField("Label", LocationMsg.class, "label");
        }
        return locationStream;
    }
    
    private XStream initLinkStream(){
        if(linkStream == null){
            linkStream = new XStream(new DomDriver());
            linkStream.alias("xml", LinkMsg.class);
            linkStream.aliasField("ToUserName", LinkMsg.class, "toUserName");
            linkStream.aliasField("FromUserName", LinkMsg.class, "fromUserName");
            linkStream.aliasField("CreateTime", LinkMsg.class, "createTime");
            linkStream.aliasField("MsgType", LinkMsg.class, "msgType");
            linkStream.aliasField("MsgId", LinkMsg.class, "msgId");
            linkStream.aliasField("Title", LinkMsg.class, "title");
            linkStream.aliasField("Description", LinkMsg.class, "description");
            linkStream.aliasField("Url", LinkMsg.class, "url");
        }
        return linkStream;
    }
    
    private XStream initVoiceStream(){
        if(voiceStream == null){
            voiceStream = new XStream(new DomDriver());
            voiceStream.alias("xml", SoundMsg.class);
            voiceStream.aliasField("ToUserName", SoundMsg.class, "toUserName");
            voiceStream.aliasField("FromUserName", SoundMsg.class, "fromUserName");
            voiceStream.aliasField("CreateTime", SoundMsg.class, "createTime");
            voiceStream.aliasField("MsgType", SoundMsg.class, "msgType");
            voiceStream.aliasField("MediaId", SoundMsg.class, "mediaId");
            voiceStream.aliasField("Format", SoundMsg.class, "format");
            voiceStream.aliasField("MsgId", SoundMsg.class, "msgId");
            voiceStream.aliasField("Recognition", SoundMsg.class, "recognition");
        }
        return voiceStream;
    }
}
